//
//  ZYCDutyRecordBulletiinDetailCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/6.
//

#import "ZYCDutyRecordBulletiinDetailCell.h"
#import "ZYCCollectionImageCell.h"
@interface ZYCDutyRecordBulletiinDetailCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contenLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (strong, nonatomic) MASConstraint *heightConstraint;
@end
@implementation ZYCDutyRecordBulletiinDetailCell

- (void)setModel:(ZYCBusRecordReportsModel *)model{
    _model = model;
    _titleLabel.text = model.quotaName;
    _contenLabel.text = model.content;

    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    
    // 由于这里collection的高度是动态的，这里cell的高度我们根据collection来计算
      CGSize collectionSize = self.collectionView.collectionViewLayout.collectionViewContentSize;
      CGFloat cotentViewH = collectionSize.height;
    [self.heightConstraint uninstall];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.heightConstraint =  make.height.mas_equalTo(cotentViewH);
    }];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contenLabel];
        [self.contentView addSubview:self.collectionView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.5f);
            make.top.equalTo(self.contentView).offset(20.0f);
        }];
        [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.left.equalTo(self.contentView).offset(15.5f);
            make.right.equalTo(self.contentView).offset(-15.5f);
        }];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contenLabel.mas_bottom).offset(15.0f);
            make.left.equalTo(self.contentView).offset(15.5f);
            make.right.equalTo(self.contentView).offset(-15.5f);
            make.bottom.equalTo(self.contentView).offset(-15.0f);
            self.heightConstraint =  make.height.mas_equalTo(150);
        }];
        
    }
    return self;
}



#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.busFileList.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCCollectionImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCCollectionImageCell class]) forIndexPath:indexPath];
    NSArray *array = self.model.busFileList;
    cell.model = array[indexPath.item];
    return cell;

}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kSystemBoldFont(17.0f) titleColor:HEX_RGB(@"#262A28") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UILabel *)contenLabel {
    if (!_contenLabel) {
        _contenLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 14.0f) titleColor:HEX_RGB(@"#888888") textAlignment:ZZTextAlignmentLeft];
    }
    return _contenLabel;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = YES;
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([ZYCCollectionImageCell class]) bundle:nil];
        [_collectionView registerNib:nib forCellWithReuseIdentifier:NSStringFromClass([ZYCCollectionImageCell class])];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat w = kScreenWidth - 15.5 *2;
        _layout.itemSize = CGSizeMake(w,w + 20);
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.minimumLineSpacing = 0.0;//行间距
        _layout.minimumInteritemSpacing = 0.0f;//列间距
    }
    return _layout;
}

@end
